<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Models\Typeinfo;
class ExpertController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
       
       //获取要添加的数据
       //$users_id = $request->session()->get('user_id');
       
      // $city_id = DB::table('city')->select('id')->where('user_id',$users_id)->first();
        
       $list =   DB::table('type_info')
               ->leftJoin('hos_list','type_info.hos_id','=','hos_list.id')
               ->orderBy('type_info.is_top','DESC')
               ->get();
       return  view('admin.expert.index',['list'=>$list]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $Info  = DB::table('hos_list')->get();
        
        return view('admin.expert.add',['list'=>$Info]);
        
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
          
        /// $users_id = $request->session()->get('user_id');
       
        // $city_id = DB::table('city')->select('id')->where('user_id',$users_id)->first();
         
         
          $data['name']  = request('name');
          $data['num']  = request('num');
          $data['job']  = request('job');
          $data['content']  = request('content');
          $data['created_at'] = date('Y-m-d H:i:s');
          $data['honor_1'] =   request('honor_1');
          $data['honor_2'] =   request('honor_2');
           $data['hos_id'] =   request('hid');
        //  $data['city_id']  = $city_id->id;
          
          if(!$request->hasFile('head_img')){
                 $request->session()->flash('error_msg','文件不存在');
                 return back();
             }
            $img = $request->file('head_img');
            // 获取后缀名
            $ext = $img->extension();
            // 新文件名
            $saveName =time().rand().".".$ext;
            // 存储文件 已经不使用 move 这种方式
            // $img->move('./uploads/'.date('Ymd'),$saveName);
            // 使用 store 存储文件
            $data['head_img'] = $img->store(date('Ymd'));
            
            
        //执行添加
        $id = \DB::table("type_info")->insertGetId($data);
        //判断
        if($id>0){
            $info = "类别详情添加成功！";
        }else{
            $info = "类别详情添加失败！";
        }
        
        return redirect("expert")->with("err",$info);
    }

    
       public function null_top(Request $request)
       {
         $id = request('id');
        
         $info = Typeinfo::find($id);

         $info->is_top  = 0;

        $status = $info->save();

         if($status){
                   return back()->with('err','推荐成功');
              }else{
                   return back()->with('err','推荐失败');
        }
       }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
      
        
       $info = \DB::table('type_info')->where('id',$id)->first();
        
       return view('admin.expert.edit',['info'=>$info]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
          $data['name']  = request('name');
          $data['job']  = request('job');
          $data['content']  = request('content');
          $data['created_at'] = date('Y-m-d H:i:s');
          $data['honor_1'] =   request('honor_1');
          $data['honor_2'] =   request('honor_2');
         if($request->hasFile('head_img')){
                $img = $request->file('head_img');
                // 获取后缀名
                $ext = $img->extension();
                // 新文件名
                $saveName =time().rand().".".$ext;
                // 存储文件 已经不使用 move 这种方式
                // $img->move('./uploads/'.date('Ymd'),$saveName);
                // 使用 store 存储文件
                $data['head_img'] = $img->store(date('Ymd'));
             }else{
                   $info = \DB::table("type_info")->where("id",$id)->first();
                   $data['head_img']  = $info->head_img;
               
             }
             $data['updated_at'] = date('Y-m-d H:i:s');
             
        $id = \DB::table("type_info")->where("id",$id)->update($data);
        if($id>0){
            return redirect('expert');
        }else{
            return back()->with("err","修改失败!");
        }
       
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
